package com.adm.dao;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.yfsoft.comm.util.BizResult;
import org.yfsoft.comm.util.StringUtil;

public class CommonDao {

	private CommonMapper mapper = null;

	public void setMapper(Object mapper) {
		this.mapper = (CommonMapper)mapper;
	}
	
	/*
	 * 查询table的字段信息
	 */
	public BizResult desc(String table){
		BizResult rst = new BizResult();
		rst.setCode("0").setData(mapper.list("show full fields from "+table));
		return rst;
	}
	/*
	 * 列出表的信息
	 */
	public BizResult table(){
		BizResult rst = new BizResult();
		rst.setCode("0").setData(mapper.list("SHOW TABLE STATUS"));
		return rst;
	}
	
	/**
	 * 返回为单行数据
	 * @return
	 */
	public BizResult query(String table,String[] fields,String where){
		BizResult rst = new BizResult();
		if(StringUtil.isNullOrEmpty(table)){
			return rst.setCode("-1").setMsg("Table Is Empty!");
		}
		String sql = "select {0} from {1} where {2}";
		String cols = "*";
		if(fields!=null && fields.length>0){
			cols = StringUtil.trim(Arrays.toString(fields), 5);
		}
		if(StringUtil.isNullOrEmpty(where)){
			where = "1 = 1";
		}
		sql = StringUtil.formatSql(sql, cols,table,where);
//		System.out.println(sql);
		rst.setCode("0").setData(mapper.query(sql));
		return rst;
	}
	
	public BizResult query(String sql){
		BizResult rst = new BizResult();
		return rst.setCode("0").setData(this.mapper.query(sql));
	}
	public BizResult list(String sql){
		BizResult rst = new BizResult();
		return rst.setCode("0").setData(this.mapper.list(sql));
	}
	/**
	 * 批量获取数据
	 * @param table 表
	 * @param fields 字段
	 * @param max 分页
	 * @param page
	 * @return
	 */
	public BizResult list(String table,String[] fields,String where,String order,int max,int page){
		BizResult rst = new BizResult();
		if(StringUtil.isNullOrEmpty(table)){
			return rst.setCode("-1").setMsg("Table Is Empty!");
		}
		String sql = "select {0} from {1} where {2} {3} limit {4},{5}";
		String cols = "*";
		if(fields!=null && fields.length>0){
			cols = StringUtil.trim(Arrays.toString(fields), 5);
		}
		if(StringUtil.isNullOrEmpty(where)){
			where = "1 = 1";
		}
		if(!StringUtil.isNullOrEmpty(order)){
			order = " order by "+ order;
		}else{
			order = "";
		}
		int l = page * max;
		int r = l + max;
		sql = StringUtil.formatSql(sql, cols,table,where,order,l,r);
//		System.out.println(sql);
		rst.setCode("0").setData(mapper.list(sql));
		return rst;
	}
	
	public BizResult injoin(String[] tables,String[] fields,String on,String where,String order,int max,int page){
		BizResult rst = new BizResult();
		if(tables==null || tables.length<1){
			return rst.setCode("-1").setMsg("Tables Can't Be Empty!");
		}
		String sql = "select {0} from {1} where {2} and {3} {4} limit {5},{6}";
		String cols = "*",table = "";
		table = StringUtil.trim(Arrays.toString(tables), 5);
		if(fields!=null && fields.length>0){
			cols = StringUtil.trim(Arrays.toString(fields), 5);
		}
		if(StringUtil.isNullOrEmpty(on)){
			return rst.setCode("-1").setMsg("ON Condition Can't Be Empty!");
		}
		if(StringUtil.isNullOrEmpty(where)){
			where = "1 = 1";
		}
		if(!StringUtil.isNullOrEmpty(order)){
			order = " order by "+ order;
		}else{
			order = "";
		}
		int l = page * max;
		int r = l + max;
		sql = StringUtil.formatSql(sql, cols,table,on,where,order,l,r);
//		System.out.println(sql);
		rst.setCode("0").setData(mapper.list(sql));
		return rst;
	}
	
	/**
	 * 删除
	 * @param table 表
	 * @param where 条件
	 * @return
	 */
	public BizResult delete(String table,String where){
		BizResult rst = new BizResult().setCode("-1");
		String sql = "delete from {0} where {1}";
		sql = StringUtil.format(sql,table, where);
		int rows = this.mapper.execute(sql);
//		System.out.println("DELETE:"+sql);
		return rst.setCode("0").setData(rows);
	}
	
	public BizResult update(Map<String,String> e,String where ,String table){
		BizResult rst = new BizResult().setCode("-1");
		String sql = "desc "+table;
		String f,t,k;
		List<Map<String,Object>> list = this.mapper.list(sql);
		sql = "update {0} set {1} where {2}";
		StringBuffer cols = new StringBuffer();
		for(Map<String,Object> o : list){
			k = o.get("Key").toString();//主键不能修改
			if("PRI".equals(k)) continue;
			f = o.get("Field").toString();//字段名称
			if(!e.containsKey(f)) continue;//未设置字段
			cols.append(f).append("=");
			t = o.get("Type").toString();//字段类型
			if(t.indexOf("int")<0){
				//字段未非整形
				cols.append("'").append(e.get(f)).append("'").append(",");
			}else{
				cols.append(e.get(f)).append(",");
			}
		}
		cols = cols.deleteCharAt(cols.length()-1);
		if(StringUtil.isNullOrEmpty(where))
			where = "1 = 1";
		sql = StringUtil.formatSql(sql,table, cols.toString(),where);
//		System.out.println(sql);
		int rows = this.mapper.execute(sql);
		return rst.setCode("0").setData(rows);
	}
	
	
	/**
	 * 插入数据
	 * @param e
	 * @param table
	 * @return
	 */
	public BizResult insert(Map<String,String> e,String table){
		BizResult rst = new BizResult().setCode("-1");
		String sql = "desc "+table;
		String f,t;
		List<Map<String,Object>> list = this.mapper.list(sql);
		sql = "insert into {0}({1}) values({2})";
		StringBuffer cols = new StringBuffer();
		StringBuffer vals = new StringBuffer();
		for(Map<String,Object> o : list){
			f = o.get("Field").toString();//字段名称
			if(!e.containsKey(f)) continue;//未设置字段
			cols.append(f).append(",");
			t = o.get("Type").toString();//字段类型
			if(t.indexOf("int")<0){
				//字段未非整形
				vals.append("'").append(e.get(f)).append("'").append(",");
			}else{
				vals.append(e.get(f)).append(",");
			}
		}
		cols = cols.deleteCharAt(cols.length()-1);
		vals = vals.deleteCharAt(vals.length()-1);
		sql = StringUtil.formatSql(sql,table, cols.toString(),vals.toString());
//		System.out.println(sql);
		int rows = this.mapper.execute(sql);
		if(rows==1){
			rst.setCode("0");
		}
		return rst;
	}
	
	/*
	<form class="form" method="post" action="<%=request.getContextPath()%>/c?s=model&o=add">
	<fieldset>
		<legend>添加</legend>
		<div class="form-line">
			<label>名称:</label>
			<span class="input-200"><input type="text" name="m_name" class="field" /></span>
			<label>标题:</label>
			<span class="input-200"><input type="text" name="m_title" class="field" /></span>
		</div>
		<div class="cl"></div>
		<div class="form-line">
			<label>激活:</label>
			<span class="input-200">
			<input type="radio" name="m_enable" value="1" class="radio" /><span class="checktxt">Y</span>
			<input type="radio" name="m_enable"  value="0" class="radio" /><span  class="checktxt">N</span></span>
			<label>JavaBean:</label>
			<span class="input-200"><input type="text" name="m_classname" class="field" /></span>
		</div>
		<div class="cl"></div>
		<div class="form-line">
			<button class="btn">保存</button>
		</div>
		<div class="cl"></div>
	</fieldset>
</form>
	 */
	public StringBuffer parseFormHTML(String action,Map<String,String> model,List<Map<String,String>> controls){
		StringBuffer sb = new StringBuffer();
		sb.append("<form class='").append(model.get("m_attr_class")).append("' method='post' action='/OA/c?").append(model.get(action)).append("' id='");
		sb.append(model.get("m_attr_id")).append("' ").append(model.get("m_attr_other")).append(" >");
		sb.append("<fieldset><legend>").append(action).append("</legend>");
//		for(Map<String,String> c:controls){
//			sb.append("<div class='form-line'>");
//			
//			sb.append("</div><div class='cl'></div>");
//		}
		sb.append("<div class='form-line'><button class='btn'>保存</button></div><div class='cl'></div></fieldset></form>");
		
		return sb;
	}
}
